package com.song.oscsong.adapter;

import java.util.ArrayList;
import java.util.List;

import com.song.oscsong.R;
import com.song.oscsong.widget.PagerSlidingTabStrip;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class ViewPageFragmentAdapter extends FragmentStatePagerAdapter {
	
	private final ViewPager mViewPager  ;
	private List<ViewPageInfo> mTabs =  new ArrayList<ViewPageInfo>();
	private final Context mContext;
	private PagerSlidingTabStrip mTabStrip;
	
	public ViewPageFragmentAdapter(FragmentManager fm , PagerSlidingTabStrip tabStrip  ,ViewPager pager) {
		super(fm);
		mContext = pager.getContext();
		mViewPager = pager;
		mTabStrip = tabStrip;
		mViewPager.setAdapter( this);
	}

	public void addTab( String title , String tag , Class<?> clss , Bundle args)
	{
		ViewPageInfo info =  new ViewPageInfo(title, tag, clss, args);
		addFragment(info);
	
	}
	
	private void addFragment(ViewPageInfo info) {
		
		if(info == null)
		{
			return ;
		}
		View v  = LayoutInflater.from(mContext).inflate(R.layout.base_viewpage_fragment_tab_item, null,false);
		
		TextView title = (TextView) v.findViewById(R.id.tab_title);
		title.setText(info.title);
		mTabStrip.addTab(v);
		mTabs.add(info);
	}

	@Override
	public Fragment getItem(int position) {
		
		ViewPageInfo info = mTabs.get(position);
		
		return Fragment.instantiate(mContext, info.clss.getName());
		
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mTabs.size();
	}

}
